// $codepro.audit.disable
package edu.gatech.oad.cyberdoc.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import edu.gatech.oad.cyberdoc.presenter.Presenter;

public class CreateUserDialog extends JDialog implements ActionListener {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtName;
	private JPasswordField pwdPassword;
	private JPasswordField pwdConfirm;
	private JComboBox comboBox;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			CreateUserDialog dialog = new CreateUserDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public CreateUserDialog() {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel lblCreateUser = new JLabel("Create User");
		lblCreateUser.setFont(new Font("Lucida Grande", Font.BOLD, 14));
		lblCreateUser.setBounds(6, 6, 101, 16);
		contentPanel.add(lblCreateUser);

		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(6, 34, 61, 16);
		contentPanel.add(lblName);

		txtName = new JTextField();
		txtName.setBounds(91, 28, 134, 28);
		contentPanel.add(txtName);
		txtName.setColumns(10);

		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] { "Patient",
				"Nurse", "Doctor", "Admin" }));
		comboBox.setBounds(91, 68, 134, 27);
		contentPanel.add(comboBox);

		JLabel lblType = new JLabel("Type:");
		lblType.setBounds(6, 73, 61, 16);
		contentPanel.add(lblType);

		pwdPassword = new JPasswordField();
		pwdPassword.setBounds(91, 102, 134, 28);
		contentPanel.add(pwdPassword);

		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(6, 108, 73, 16);
		contentPanel.add(lblPassword);

		pwdConfirm = new JPasswordField();
		pwdConfirm.setBounds(91, 138, 134, 28);
		contentPanel.add(pwdConfirm);

		JLabel lblConfirm = new JLabel("Confirm:");
		lblConfirm.setBounds(6, 144, 61, 16);
		contentPanel.add(lblConfirm);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(this);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(this);
				buttonPane.add(cancelButton);
			}
		}

		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("OK")) {
			if (txtName.getText().length() == 0) {
				JOptionPane.showMessageDialog(this, "Name cannot be empty.",
						"Invalid Name", JOptionPane.ERROR_MESSAGE);
			} else if (pwdPassword.getPassword().length == 0) {
				JOptionPane.showMessageDialog(this,
						"Password cannot be empty.", "Invalid Password",
						JOptionPane.ERROR_MESSAGE);
			} else if (!Arrays.equals(pwdPassword.getPassword(),
					pwdConfirm.getPassword())) {
				JOptionPane.showMessageDialog(this, "Passwords do not match.",
						"Invalid Password", JOptionPane.ERROR_MESSAGE);
			} else {
				Presenter.getInstance().createUser(txtName.getText(),
						(String) comboBox.getSelectedItem(),
						new String(pwdPassword.getPassword()));
				setVisible(false);
			}
		} else {
			setVisible(false);
		}
	}
}
